package com.seeyon.apps.cxgd.kit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.ctp.common.formula.FormulaUtil;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.shuqi.hack.StandardCharsets;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class SameBusinessTripKit {

    private static final Log log = CtpLogFactory.getLog(SameBusinessTripKit.class);

    public static String getSameBusinessTripToken(){
        try {
            String same_business_trip_url = StrKit.str(FormulaUtil.getVar("same_business_trip_url"));
            String account = StrKit.str(FormulaUtil.getVar("tcAccount"));
            String password = StrKit.str(FormulaUtil.getVar("tcPassword"));
//            String same_business_trip_url = "https://api.qa.dttrip.cn";
//            String account = "54437ee10f3e4de1aeb866da3cb4fc12";
//            String password = "yPJiHqROqvWxrX0JDGRO";
            String tokenUrl = same_business_trip_url+"/openapi/api/OAuth/v2/GetAccessToken";
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("random", UUID.randomUUID().toString());
            map.put("timestamp", System.currentTimeMillis());
            Map<String, Object> param_map = new HashMap<String, Object>();
            param_map.put("account", account);
            param_map.put("password", password);
            param_map.put("tokenType", "");
            map.put("param", param_map);
            String token_json = JSON.toJSONString(map);
            String result = HttpKit.httpsPost(tokenUrl, new HashMap<>(), token_json);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String success = StrKit.str(jsonObject.get("success"));
            String accessToken = "";
            if("true".equals(success)){
                JSONObject data_obj = jsonObject.getJSONObject("data");
                accessToken = StrKit.str(data_obj.get("accessToken"));
            }
            return accessToken;
        } catch (Exception e) {
            log.error("获取同程商旅token失败:", e);
        }
        return "";
    }

    public static String str(Object o) {
        if (o == null) {
            // 如果传入的对象为空，则返回一个空字符串
            return "";
        }
        if (o instanceof String) {
            // 如果传入的对象是字符串类型，则将其返回
            return (String) o;
        }
        if (o instanceof Date) {
            // 如果传入的对象是日期类型，则将其转为字符串并使用指定格式进行格式化后返回
            return new SimpleDateFormat("yyyyMMdd").format((Date) o);
        }
        // 如果传入的对象不属于上述类型，则调用其toString()方法将其转为字符串后返回
        return o.toString().trim();

    }

    public static String stringToMD5(Object param) {
        if (param == null) {
            return "";
        } else {
            String result = null;
            MessageDigest md5 = null;
            try {
                md5 = MessageDigest.getInstance("MD5");
                byte[] input = str(param).getBytes(StandardCharsets.UTF_8);
                byte[] output = md5.digest(input);
                result = Hex.encodeHexString(output).toLowerCase();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return result;
        }
    }
}
